﻿using System;
using System.Data;
using System.Collections.Generic;

using System.Text;
using Portal.Environment;
using Portal.Language;
using Portal.Interface;

namespace Portal.CDSP
{
    public class Vote : WebPart
    {
        public override string WebPartId
        {
            get
            {
                return "WebSite.Vote";
            }
        }
        public override string WebPartFolder
        {
            get
            {
                return "Portal.CDSP";
            }
        }

        public override string Title
        {
            get
            {
                return GetText("Title", "Thăm dò ý kiến");
            }
        }

        public override ViewCls[] Views
        {
            get
            {
                return new ViewCls[]{
                    new ViewCls("DefaultView",GetText("DefaultView","Ngầm định (Chuẩn)"),1)
                };
            }
        }


        public override string NameSpace
        {
            get
            {
                return "Portal.CDSP";
            }
        }


        [AjaxPro.AjaxMethod()]
        public string GetHtml(string TabModuleId, int CheckedIndex)
        {
            try
            {
                TabModuleCls TabModuleCls = new TabModuleBLL().CreateModelFromXml(TabModuleId);
                string TempId = TabModuleId.Substring(0, 8);
                VoteItemCls[] VoteItems = null;
                string Question = new VoteBLL().GetInfo(Environments.ActiveLanguage, TabModuleCls.Id, ref VoteItems);
                string Html = WebPartUtilities.AttachJavascript(WebPartFolder, "Vote.js");
                Html += "<div class=\"side poll\">\n" +
                        "					<div class=\"side-head\">\n" +
                        "						<h2>Thăm dò dư luận</h2>\n" +
                        "					</div>\n" +
                        "					<div class=\"side-content\">\n" +
                        "						<p>" + Question + "</p>\n";

                if (VoteItems != null)
                {

                    Html += "<input type=hidden id=divMaxVote_" + TabModuleId + " value=\"" + VoteItems.Length + "\">";
                    for (int iIndex = 0; iIndex < VoteItems.Length; iIndex++)
                    {
                        if (CheckedIndex == iIndex)
                        {
                            Html += "<p><input type=hidden id=txtVoteId_" + TabModuleId + "_" + iIndex + " value=\"" + VoteItems[iIndex].VoteItemId + "\"><input type=radio name=\"answer\" checked id=checkVote_" + TabModuleId + "_" + iIndex + "><span id=txtVoteName_" + TabModuleId + "_" + iIndex + ">" + VoteItems[iIndex].VoteItemName + "</span></p>";
                        }
                        else
                        {
                            Html += "<p><input type=hidden id=txtVoteId_" + TabModuleId + "_" + iIndex + " value=\"" + VoteItems[iIndex].VoteItemId + "\"><input onclick=\"javascript:ChangeOptionVote('" + TabModuleId + "'," + iIndex + ");\" type=radio name=\"answer\" id=checkVote_" + TabModuleId + "_" + iIndex + "><span id=txtVoteName_" + TabModuleId + "_" + iIndex + ">" + VoteItems[iIndex].VoteItemName + "</span></p>";
                        }
                    }
                    Html +=
                        "   <a href=\"#\" class=\"vote\" onclick=javascript:ClientVoteFunc(\"" + TabModuleCls.TabModuleId + "\");>Bỏ phiếu</a> " +
                        "   <a href=# class=\"poll-result\" onclick=javascript:ResultVoteFunc(\"" + TabModuleCls.Id + "\");>  Xem kết quả</a>";
                }
                Html += "					</div>\n" +
                        "				</div>\n";
                return Html;
            }
            catch (Exception ex)
            {
                return ex.Message.ToString();
            }
        }

        private string ClientRenderWebPart(string TabModuleId, string HasPorlet)
        {
            try
            {
                TabModuleCls TabModuleCls = new TabModuleBLL().CreateModelFromXml(TabModuleId);
                string Html = "<div id=divVoteContent" + TabModuleId + ">" + GetHtml(TabModuleId, -1) + "</div>";
                return Html;
            }
            catch (Exception ex)
            {
                return ex.Message.ToString();
            }
        }

        [AjaxPro.AjaxMethod]
        public override string ClientRenderWebPart(string BelongSite, string TabModuleId, string ViewName, string HasPorlet, LoadParamCls loadParamCls)
        {
            if (ViewName.Equals("DefaultView"))
            {
                return ClientRenderWebPart(TabModuleId, HasPorlet);
            }
            return GetText("ViewNotFound", "Không tìm thấy View [" + ViewName + "]");
        }

        [AjaxPro.AjaxMethod]
        public string GetVoteClicked(string Subject)
        {
            string ClickedName = GetText("ClickedVote", "Bạn đã bỏ phiếu cho <b> $ClickedName$ </b>");
            ClickedName = ClickedName.Replace("$ClickedName$", "[" + Subject + "]");
            return ClickedName;
        }


        [AjaxPro.AjaxMethod]
        public string IncVoteItem(string Id)
        {
            try
            {
                new VoteBLL().IncVoteItem(null, Id);
                return "";
            }
            catch (Exception ex)
            {
                return "ERROR: " + ex.Message.ToString();
            }
        }

        public override DataTable ReadingDataService(string belongSite, string ActiveLanguage, ref string IdField, ref string SubjectField)
        {
            IdField = "Id";
            SubjectField = "Subject";
            return new VoteBLL().ReadingForService();
        }

        public override bool AllowFrontService
        {
            get
            {
                return true;
            }
        }

        public override void RegAjaxPro(System.Web.UI.Page ActivePage)
        {
            AjaxPro.Utility.RegisterTypeForAjax(typeof(Portal.CDSP.Vote), ActivePage);
        }

        [AjaxPro.AjaxMethod]
        public string ShowResultForm(string VoteId)
        {
            string ResultUrl = Screen.BuildDialogUrl(new FResultVoteWP().WebPartId, new ParamCls[] { new ParamCls("VoteId", VoteId) });
            return ResultUrl;
        }
    }
}
